package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 数据统计服务
 */

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Autowired
    private SetmealDao setmealDao;

    //查询运用数据

    /**
     * reportDate:null,
     * todayNewMember :0,
     * totalMember :0,
     * thisWeekNewMember :0,
     * thisMonthNewMember :0,
     * todayOrderNumber :0,
     * todayVisitsNumber :0,
     * thisWeekOrderNumber :0,
     * thisWeekVisitsNumber :0,
     * thisMonthOrderNumber :0,
     * thisMonthVisitsNumber :0,
     * hotSetmeal :[
     * {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
     * {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
     * ]
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {

        //获得当前系统时间
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //动态获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //动态获得本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);//select count(id) from t_member where regTime = today

        //总会员数据
        Integer totalMember = memberDao.findMemberTotalCount();

        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);

        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);

        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);

        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);

        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);

        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);

        return result;
    }


    @Override
    public Map<String, Object> findCountByDate(String beginDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        List<String> days = orderDao.findDays(beginDate, endDate);
        map.put("days", days);
        List<String> orderCount = new ArrayList();
        for (String day1 : days) {
            String orderCountByDate = orderDao.findOrderCount(day1);
            orderCount.add(orderCountByDate);
        }
        map.put("orderCount", orderCount);
        List<String> actuallyComeCount = new ArrayList();
        for (String day2 : days) {
            String actuallyComeCountByDay = orderDao.findOrderCountByDay(day2);
            actuallyComeCount.add(actuallyComeCountByDay);
        }
        map.put("actuallyComeCount", actuallyComeCount);
        return map;
    }


    //体检收入
    @Override
    public Map<String, Object> findIncomeByDate(String beginDate, String endDate) {
        // 要返回到前端页面的数据map
        Map<String, Object> map = new HashMap<>();
        // 存储饼形图的数据:value值和套餐名
        List<Map> valueAndNames = new ArrayList();
        // 根据日期查找预约表中的套餐id
        List<String> setmealIds = orderDao.findSetMealIds(beginDate, endDate);

        // 遍历套餐id查找套餐表中的套餐名
        List<String> setmealNames = new ArrayList<>();


        for (String setmealId1 : setmealIds) {
            String name = setmealDao.getSetmealNames(setmealId1);
            // 将套餐名存储到setmealNames集合中
            setmealNames.add(name);
            // 根据日期查询套餐出现的次数
            Integer countSetmealId = orderDao.getCountSetmealId(beginDate, endDate, setmealId1);

            // 获取套餐的价格
            Integer setmealPrice = setmealDao.getPriceBySetmealId(setmealId1);
            //获取套餐的收入
            Integer value = countSetmealId * setmealPrice;
            // 每次查找之后将收入和套餐名保存到map3中
            Map<String, Object> map3 = new HashMap<>();
            map3.put("value", value);
            map3.put("name", name);
            valueAndNames.add(map3);
        }
        // 总收入:套餐的价格 = 套餐各个价格相加 * 套餐的价格
        int sum = 0;
        for (Map valueAndName : valueAndNames) {
            Integer value = (Integer) valueAndName.get("value");
            sum += value;
        }
        String s = String.valueOf(sum);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("value", s);
        map4.put("name", "总收入");
        valueAndNames.add(map4);

        setmealNames.add("总收入");
        map.put("setmealNames", setmealNames);
        map.put("valueAndNames", valueAndNames);

        return map;
    }

    @Override
    public List<Integer> getMemberReportAge() throws Exception {
        List<Member> list = memberDao.findAll();
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;
        for (Member member : list) {
            Date birthday = member.getBirthday();
            String s = DateUtils.parseDate2String(birthday);
            String age = s.substring(0,4);
            int h = Integer.parseInt(age);
            int i = 2020 - h;
            if (i >= 0 && i <= 6) {
                a++;
            }
            if (7 >= 0 && i <= 12) {
                b++;
            }
            if (13 >= 0 && i <= 17) {
                c++;
            }
            if (18 >= 0 && i <= 45) {
                d++;
            }
            if (46 >= 0 && i <= 69) {
                e++;
            }
            if (i > 69) {
                f++;
            }
        }
        List<Integer> ages= new ArrayList<>();
        ages.add(a);
        ages.add(b);
        ages.add(c);
        ages.add(d);
        ages.add(e);
        ages.add(f);
        return ages;
    }
}
